//
//  QDMetroCodeHeaderView.m
//  QDMetroClient
//
//  Created by YueWen on 2018/12/28.
//  Copyright © 2018 YueWen. All rights reserved.
//

#import "QDMetroCodeHeaderView.h"

@interface QDMetroCodeHeaderView ()

/// 负责切换方式的control
@property (nonatomic, strong) UIControl *typeControl;

/// 背景色
@property (nonatomic, strong) UIColor *backgroundColor;

@end

@implementation QDMetroCodeHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = RITLColorFromIntRBG(100, 181, 116)/*QDSColorTheme*/;
        
        /// 实例化视图
        self.codeImageView = ({
            
            UIImageView *imageView = [UIImageView new];
            imageView.backgroundColor = UIColor.whiteColor;
            
            imageView;
        });
        
        
        self.signImageView = ({
            
            UIImageView *imageView = [UIImageView new];
            imageView.backgroundColor = UIColor.whiteColor;
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.image = @"mc_bus_sign".ritl_image;
            imageView.hidden = true;
            
            imageView;
        });
        
        
        self.titleLabel = ({
            
            UILabel *label = [UILabel new];
            label.backgroundColor = [UIColor whiteColor];
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = QDSColorDefaultSeparator;
            label.font = RITLSemiboldFont(14);
            
            label;
        });
        
        
        self.subtitleLabel = ({
            
            UILabel *label = [UILabel new];
//            label.backgroundColor = RITLColorFromIntRBG(255, 249, 235);
            label.backgroundColor = UIColor.whiteColor;
            label.layer.cornerRadius = 1;
            label.clipsToBounds = true;
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor = RITLColorFromIntRBG(255, 157, 0);
            label.font = RITLRegularFont(14);
            
            label;
        });
        
        // 辅助layer定位的
        CGFloat width = UIScreen.mainScreen.ritl_width;
        CGFloat cornerOriginY = 323 + QDAddtionTopDistance() + QDAddtionMiddleDistance();
        CGFloat rightOrginX = width - 20 * 2 - 5;
        
        // 左右两个半圆
        CALayer *leftLayer = CALayer.layer;
        CALayer *rightLayer = CALayer.layer;
        leftLayer.cornerRadius = rightLayer.cornerRadius = 6;
        leftLayer.anchorPoint = rightLayer.anchorPoint = CGPointZero;
        leftLayer.bounds = rightLayer.bounds = CGRectMake(0, 0, 12, 12);
        leftLayer.backgroundColor = rightLayer.backgroundColor = self.backgroundColor.CGColor;
        leftLayer.position = CGPointMake(-7, cornerOriginY);
        rightLayer.position = CGPointMake(rightOrginX, cornerOriginY);
        
        [self.contentView.layer addSublayer:leftLayer];
        [self.contentView.layer addSublayer:rightLayer];
        
        // 中间的虚线
        CAShapeLayer *lineLayer = CAShapeLayer.new;
        lineLayer.strokeColor = RITLColorSimpleFromIntRBG(219).CGColor;
        lineLayer.fillColor = UIColor.clearColor.CGColor;
        lineLayer.anchorPoint = CGPointZero;
        CGRect rect = CGRectMake(19, 329 + QDAddtionTopDistance() + QDAddtionMiddleDistance(), width - 37.5 * 2, 0.5);
        lineLayer.path = [UIBezierPath bezierPathWithRect:rect].CGPath;
        lineLayer.lineWidth = 0.5;
        lineLayer.strokeStart = 0.001;
        lineLayer.strokeEnd = 0.499;
        lineLayer.lineDashPattern = @[@4];
        [self.contentView.layer addSublayer:lineLayer];
        
        self.typeImageView = ({
            
            UIImageView *imageView = [UIImageView new];
            imageView.backgroundColor = UIColor.whiteColor;
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            
            imageView;
        });
        
        self.typeTitleLabel = ({
            
            UILabel *label = [UILabel new];
            label.backgroundColor = [UIColor whiteColor];
            label.text = @"支付宝";
            label.font = RITLSemiboldFont(14);
            label.textColor = QDSColorDefaultSeparator;
            
            label;
        });
        
        self.typeDescLabel = ({
            
            UILabel *label = [UILabel new];
            label.backgroundColor = [UIColor whiteColor];
            label.font = RITLRegularFont(12);
            label.textColor = QDSColorDefaultGray2;
            
            label;
        });
        
        self.arrowButton = ({
            
            UIButton *view = [UIButton new];
            view.adjustsImageWhenHighlighted = false;
            view.adjustsImageWhenDisabled = false;
            view.backgroundColor = UIColor.whiteColor;
            [view setImage:@"mc_arrow_right".ritl_image forState:UIControlStateNormal];
            
            view;
        });
        
        self.typeControl = ({
            
            UIControl *view = [UIControl new];
            view.backgroundColor = [UIColor clearColor];
            
            view;
        });
        
        /// 追加视图
        
        [self.contentView addSubview:self.signImageView];
        [self.contentView addSubview:self.codeImageView];
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.subtitleLabel];
        [self.contentView addSubview:self.typeControl];
        [self.typeControl addSubview:self.typeImageView];
        [self.typeControl addSubview:self.typeTitleLabel];
        [self.typeControl addSubview:self.typeDescLabel];
        [self.typeControl addSubview:self.arrowButton];
        
        
        /// 约束布局

        [self.codeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(52 + QDAddtionTopDistance());
            make.centerX.offset(0);
            make.width.height.mas_equalTo(210);
        }];
        
        
        [self.signImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(16);
            make.width.mas_equalTo(120);
            make.bottom.equalTo(self.codeImageView.mas_top);
            make.centerX.offset(1);
        }];
        
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.offset(0);
            make.height.mas_lessThanOrEqualTo(22);
            make.top.equalTo(self.codeImageView.mas_bottom).offset(12.2 + QDAddtionMiddleDistance());
        }];
        
        
        [self.subtitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.offset(0);
            make.height.mas_lessThanOrEqualTo(24);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(2);
            make.width.mas_equalTo(160);
        }];
        
        
        [self.typeControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.offset(0);
            make.height.mas_equalTo(66);
        }];
        
        
        [self.typeDescLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.inset(15);
            make.left.offset(60);
            make.height.mas_lessThanOrEqualTo(17);
        }];
        
        
        [self.arrowButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.width.mas_equalTo(16);
            make.bottom.inset(24);
            make.right.inset(16);
        }];
        
        
        [self.typeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(32);
            make.left.offset(20);
            make.bottom.inset(16);
        }];
        
        
        [self.typeTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(60);
            make.height.mas_equalTo(20);
            make.bottom.inset(30);
        }];
        

        self.titleLabel.text = @"二维码对准闸机口扫码进出站";
        self.subtitleLabel.text = @"刷码高度建议 5cm 以上";
        self.typeDescLabel.text = @"优先使用此支付方式支付款";
        [self.typeControl addTarget:self action:@selector(changedPayType:) forControlEvents:UIControlEventTouchUpInside];
        self.typeTitleLabel.text = @"支付宝";
    }
    
    return self;
}




/// 修改支付方式
- (void)changedPayType:(UIControl *)sender {
    
    if (!self.changeTypeHandler) { return; }
    self.changeTypeHandler();
}

@end
